package com.jiepos.mpos.common.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.stereotype.Repository;

import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.entity.App_Custom_AccountEntity;
import com.jiepos.mpos.core.dao.BaseDao;

/**
 * 
 * 会员账户dao层
 * 
 * @ProjectName: [mpos1]
 * @Package com.jiepos.mpos.common.dao
 * @author 肖志伟
 * @version 1.0
 * @since 2017年9月25日 下午2:57:50
 * @history（历次修订内容、修订人、修订时间等） @UpdateRemark [说明本次修改内容]
 * @Copyright (c) 2017 江苏杰博实信息技术有限公司
 */
@Repository
public class CustomAccountDao extends BaseDao<App_Custom_AccountEntity> {

	/**
	 * 修改会员账户状态数据库操作
	 * 
	 * @param param
	 * @param customAccountEntity
	 * @param account_uuids
	 */
	public void updateCustomAccountStByIds(String param, App_Custom_AccountEntity customAccountEntity,
			String account_uuids) {
		final String[] ids = account_uuids.split(",");
		String sql = " UPDATE " + Table.T_APP_CUSTOM_ACCOUNT + " SET account_st ='"
				+ customAccountEntity.getAccount_st() + "' , REC_UPD_TS=SYSTIMESTAMP ,rec_upd_usr_id='"
				+ customAccountEntity.getRec_upd_usr_id() + "', last_oper_in='" + SystemConstant.OPER_UPDATE
				+ "' WHERE " + param + " = ?";
		log.debug("updateByIds sql : " + sql);
		log.debug("updateByIds ids: " + ids);
		jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
			@Override
			public int getBatchSize() {
				return ids.length;
			}

			@Override
			public void setValues(PreparedStatement arg0, int arg1) throws SQLException {
				arg0.setString(1, ids[arg1]);
			}
		});

	}

	/**
	 * 调用存储过程修改状态
	 * @param custom_uuid
	 */
	public Boolean updateCustomAccountProcedure(String custom_uuids) {
		Boolean result = (Boolean)jdbcTemplate.execute(new CallableStatementCreator() {
			public CallableStatement createCallableStatement(Connection con) throws SQLException {
				String storedProc = "{call package_member.p_batch_update_account_dac(?,?)}";// 调用的sql
				CallableStatement cs = con.prepareCall(storedProc);
				cs.setString(1, custom_uuids);
				cs.setString(2, ",");
				return cs;
			}
		}, new CallableStatementCallback<Object>() {
			public Object doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException {
				return cs.execute();
			}
		});
		return result;
	}

}
